﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sgsBase;

namespace TestApp.Controls
{
	public partial class UserStatus : System.Web.UI.UserControl
	{
		protected SgsGlobal Gl;

		protected void Page_Load(object sender, EventArgs e)
		{
			Gl = (SgsGlobal)HttpContext.Current.ApplicationInstance;

			if (Gl.CurUser.ID != 0)
			{
				showLoggedIn(true);
				return;
			}

			HttpCookie cookie = Request.Cookies[ApplicationHelper.CookieGroup];

			if (cookie != null)
			{
				string code = cookie[ApplicationHelper.LoginCookie];
				if (code != null &&  Gl.CurUser.Login(code))
				{
					showLoggedIn(true);
					return;
				}
			}

			showLoggedIn(false);
		}

		private void showLoggedIn(bool visible)
		{
			LoggedIn.Visible = visible;
			Visitor.Visible = !visible;

			if (visible)
				DisplayName.Text = Gl.CurUser.FullName;
		}
	}
}